package com.johnson.news.share;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.johnson.news.R;
import com.johnson.news.event_msg.ShareMsg;
import com.johnson.news.util.Utils;
import com.johnson.news.wxapi.Util;
import com.youmbe.library.util.MyToast;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.Map;


/**
 * Created by MIN on 16/5/18.
 */
public class SharePopupWindow extends PopupWindow implements View.OnClickListener {
    Context mContext;
    private View mView;
    private TextView mQQ, mWeiXin, mWeiBo, mPengYouQuan;
    private Button mCancle;
    private ShareAppUtils mShareAppUtils;
    String mShareUrl = "", mShareTitle = "", mShareDesc = "", mShareImage = "";
    //
    private Bitmap mShareBitmap;  //分享的图片
    //
    public static int shareType = -1;  //1：qq   2:微信    3：朋友圈  4：微博
    private ShareSuccess sharesuccess;

    public SharePopupWindow(Context context, Map<String, String> shareinfo) {
        super(context);
        this.mContext = context;
        if (shareinfo != null) {

            mShareUrl = (String) shareinfo.get("url");
            mShareTitle = (String) shareinfo.get("title");
            mShareDesc = (String) shareinfo.get("content");
            if (TextUtils.isEmpty(mShareDesc)) {
                mShareDesc = (String) shareinfo.get("resume");
            }
            mShareImage = (String) shareinfo.get("thumb");

            if(TextUtils.isEmpty(mShareTitle))
                mShareTitle = mContext.getResources().getString(R.string.app_name);
            if(TextUtils.isEmpty(mShareDesc))
                mShareDesc = mContext.getResources().getString(R.string.app_name);
        } else {  //假数据
//            mShareUrl = DataUrl.REGISTER_XIEYI;
            mShareDesc = mShareTitle = mContext.getResources().getString(R.string.app_name);
        }
        initView();

    }

    public SharePopupWindow(Context context, Bitmap bitmap) {
        this.mContext = context;
        this.mShareBitmap = bitmap;
        initView();
    }

    private void initView() {
        //
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(mContext.LAYOUT_INFLATER_SERVICE);
        mView = inflater.inflate(R.layout.view_share, null);
        mQQ = mView.findViewById(R.id.share_iv_qq);
        mWeiXin = mView.findViewById(R.id.share_iv_weixin);
        mWeiBo = mView.findViewById(R.id.share_iv_weibo);
        mPengYouQuan = mView.findViewById(R.id.share_iv_pengyouquan);
        mCancle = (Button) mView.findViewById(R.id.share_btn_cancle);
        //
        mQQ.setOnClickListener(this);
        mWeiXin.setOnClickListener(this);
        mWeiBo.setOnClickListener(this);
        mPengYouQuan.setOnClickListener(this);

        mCancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();//销毁弹框
            }
        });
        //设置view
        this.setContentView(mView);
        //设置弹框的宽和高
        this.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        this.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        //设置窗体可点击
        this.setFocusable(true);
        //设置弹出动画
        this.setAnimationStyle(R.style.PopupWindowAnim);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable cd = new ColorDrawable(0xb0000000);
        // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
        this.setBackgroundDrawable(cd);
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                restoreWindowBg();

                if(EventBus.getDefault().isRegistered(this))
                    EventBus.getDefault().unregister(this);

//                try {
//                    mWXShareSuccessBroadcast.unregisterBroadcast();
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
                if(dismissedListener != null)
                    dismissedListener.onDismissed();


            }
        });
//        //
//        mWXShareSuccessBroadcast = new Coco3gBroadcastUtils(mContext);
//        mWXShareSuccessBroadcast.receiveBroadcast(Coco3gBroadcastUtils.SHARE_SUCCESS).setOnReceivebroadcastListener(new Coco3gBroadcastUtils.OnReceiveBroadcastListener() {
//            @Override
//            public void receiveReturn(Intent intent) {
//                shareSuccess();
//            }
//        });

        EventBus.getDefault().register(this);

        darkWindowBg();

    }

    void darkWindowBg() {
        if(mContext instanceof Activity) {//Activity背景变暗
            WindowManager.LayoutParams lp = ((Activity) mContext).getWindow().getAttributes();
            lp.alpha = 0.7f;
            ((Activity) mContext).getWindow().setAttributes(lp);
        }
    }

    void restoreWindowBg() {
        WindowManager.LayoutParams lp = ((Activity) mContext).getWindow().getAttributes();
        lp.alpha = 1f;
        ((Activity) mContext).getWindow().setAttributes(lp);
    }


    public void setQQShareListener() {

    }


    @Override
    public void onClick(View v) {
        int i = v.getId();// qq分享
// 微信分享
// 朋友圈分享
// 新浪微博分享
        if (i == R.id.share_iv_qq) {
            if(!Utils.isQQClientAvailable(mContext)) {
                MyToast.showToast("您还未安装qq", mContext);
                return;
            }
            shareType = 1;
            if (mShareBitmap != null) {
                mShareAppUtils = new ShareAppUtils(mContext, 1, mShareBitmap);
            } else {
                mShareAppUtils = new ShareAppUtils(mContext, 1, mShareUrl, mShareTitle, mShareDesc, mShareImage);
            }


        } else if (i == R.id.share_iv_weixin) {
            if(!Utils.isWeixinAvilible(mContext)) {
                MyToast.showToast("您还未安装微信", mContext);
                return;
            }
            shareType = 2;

            if (mShareBitmap != null) {
                mShareAppUtils = new ShareAppUtils(mContext, 2, mShareBitmap);
            } else {
                mShareAppUtils = new ShareAppUtils(mContext, 2, mShareUrl, mShareTitle, mShareDesc, mShareImage);
            }


        } else if (i == R.id.share_iv_pengyouquan) {
            if(!Utils.isWeixinAvilible(mContext)) {
                MyToast.showToast("您还未安装微信", mContext);
                return;
            }
            shareType = 3;
            if (mShareBitmap != null) {
                mShareAppUtils = new ShareAppUtils(mContext, 3, mShareBitmap);
            } else {
                mShareAppUtils = new ShareAppUtils(mContext, 3, mShareUrl, mShareTitle, mShareDesc, mShareImage);
            }


        } else if (i == R.id.share_iv_weibo) {
            if(!Utils.isWeiboClientAvailable(mContext)) {
                MyToast.showToast("您还未安装微博", mContext);
                return;
            }
            shareType = 4;
                mShareAppUtils = new ShareAppUtils(mContext, 4, mShareUrl, mShareTitle, mShareDesc, mShareImage);

        }
        dismiss();
    }

    //获取那种分享方式
    public int getmShareType() {
        return shareType;
    }

    public void setOnShareSuccessListener(ShareSuccess sharesuccess) {
        this.sharesuccess = sharesuccess;
    }

    public interface ShareSuccess {
        void OnShareSuccess();
    }

    DismissedListener dismissedListener;
    public void setDismissedListener(DismissedListener listener){
        dismissedListener = listener;
    }
    public interface DismissedListener {
        void onDismissed();
    }

    private void shareSuccess() {
        if (sharesuccess != null) {
            sharesuccess.OnShareSuccess();
        }
    }

    @Subscribe
    public void onShareFinished(ShareMsg shareMsg) {
        shareSuccess();
    }


}

